<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const posts = ref([])
const loading = ref(true)
const currentUser = ref(null)

// 从localStorage获取当前用户
const getUser = () => {
  const userStr = localStorage.getItem('user')
  return userStr ? JSON.parse(userStr) : null
}

// 获取所有帖子
const fetchPosts = () => {
  const savedPosts = localStorage.getItem('socialPosts')
  posts.value = savedPosts ? JSON.parse(savedPosts) : []
  loading.value = false
}

// 发布新帖子
const createPost = () => {
  router.push('/social/create')
}

// 查看帖子详情
const viewPostDetail = (postId) => {
  router.push(`/social/post/${postId}`)
}

// 点赞功能
const toggleLike = (postId) => {
  const postIndex = posts.value.findIndex(p => p.id === postId)
  if (postIndex !== -1) {
    const post = posts.value[postIndex]
    const likeIndex = post.likes.findIndex(like => like.username === currentUser.value.username)
    
    if (likeIndex === -1) {
      // 点赞
      post.likes.push({ username: currentUser.value.username })
    } else {
      // 取消点赞
      post.likes.splice(likeIndex, 1)
    }
    
    // 保存到localStorage
    localStorage.setItem('socialPosts', JSON.stringify(posts.value))
  }
}

// 检查是否已点赞
const isLiked = (post) => {
  if (!currentUser.value) return false
  return post.likes.some(like => like.username === currentUser.value.username)
}

// 返回首页
const goHome = () => {
  router.push('/home')
}

onMounted(() => {
  currentUser.value = getUser()
  fetchPosts()
})
</script>

<template>
  <div class="social-feed-container">
    <!-- 头部导航 -->
    <header class="social-header">
      <div class="header-content">
        <button class="back-btn" @click="goHome">←</button>
        <h1>社区动态</h1>
        <div class="header-actions">
          <button class="create-btn" @click="createPost">+ 发布</button>
        </div>
      </div>
    </header>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p>加载中...</p>
    </div>

    <!-- 帖子列表 -->
    <main v-else class="posts-feed">
      <div v-if="posts.length === 0" class="empty-state">
        <p>还没有帖子，快来发布第一条吧！</p>
        <button class="create-first-btn" @click="createPost">发布帖子</button>
      </div>
      
      <div v-for="post in posts" :key="post.id" class="post-card" @click="viewPostDetail(post.id)">
        <!-- 帖子头部 -->
        <div class="post-header">
          <div class="user-info">
            <div class="avatar">{{ post.author.charAt(0).toUpperCase() }}</div>
            <div class="username">{{ post.author }}</div>
          </div>
          <span class="post-time">{{ new Date(post.createdAt).toLocaleDateString() }}</span>
        </div>

        <!-- 帖子内容 -->
        <div class="post-content">
          <p class="post-text">{{ post.content }}</p>
          
          <!-- 图片展示 -->
          <div v-if="post.images && post.images.length > 0" class="post-images">
            <div v-if="post.images.length === 1" class="single-image">
              <img :src="post.images[0]" alt="帖子图片" class="post-img">
            </div>
            <div v-else class="multiple-images">
              <img 
                v-for="(image, index) in post.images.slice(0, 4)" 
                :key="index" 
                :src="image" 
                alt="帖子图片" 
                class="post-img small"
              >
              <div v-if="post.images.length > 4" class="more-images">+{{ post.images.length - 4 }}</div>
            </div>
          </div>

          <!-- 视频展示 -->
          <div v-if="post.video" class="post-video">
            <video :src="post.video" controls class="post-video-player"></video>
          </div>
        </div>

        <!-- 帖子互动 -->
        <div class="post-actions">
          <button 
            class="action-btn like-btn" 
            :class="{ liked: isLiked(post) }"
            @click.stop="toggleLike(post.id)"
          >
            <span class="like-icon">{{ isLiked(post) ? '❤️' : '🤍' }}</span>
            <span class="like-count">{{ post.likes.length }}</span>
          </button>
          
          <button class="action-btn comment-btn">
            <span class="comment-icon">💬</span>
            <span class="comment-count">{{ post.comments.length }}</span>
          </button>
        </div>
      </div>
    </main>
  </div>
</template>

<style scoped>
.social-feed-container {
  max-width: 100%;
  margin: 0 auto;
  background-color: #fafafa;
  min-height: 100vh;
}

.social-header {
  position: sticky;
  top: 0;
  background-color: white;
  padding: 16px 20px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  z-index: 100;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 800px;
  margin: 0 auto;
}

.back-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  padding: 4px 12px;
  border-radius: 4px;
}

.back-btn:hover {
  background-color: #f0f0f0;
}

.header-content h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

.create-btn {
  background-color: #ff0000;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 20px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
}

.create-btn:hover {
  background-color: #e60000;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: #666;
}

.loading-spinner {
  width: 30px;
  height: 30px;
  border: 3px solid #f0f0f0;
  border-top: 3px solid #ff0000;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 10px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.posts-feed {
  max-width: 800px;
  margin: 0 auto;
  padding: 16px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #666;
}

.create-first-btn {
  margin-top: 20px;
  background-color: #ff0000;
  color: white;
  border: none;
  padding: 10px 24px;
  border-radius: 24px;
  font-weight: 500;
  cursor: pointer;
}

.post-card {
  background-color: white;
  border-radius: 12px;
  margin-bottom: 20px;
  padding: 16px;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.post-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.post-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #ff6b6b;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 600;
  font-size: 16px;
}

.username {
  font-weight: 500;
  color: #333;
}

.post-time {
  font-size: 12px;
  color: #999;
}

.post-content {
  margin-bottom: 16px;
}

.post-text {
  font-size: 16px;
  line-height: 1.6;
  color: #333;
  margin-bottom: 12px;
  word-break: break-word;
}

.post-images {
  width: 100%;
}

.single-image {
  width: 100%;
  border-radius: 8px;
  overflow: hidden;
}

.multiple-images {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px;
  position: relative;
  border-radius: 8px;
  overflow: hidden;
}

.post-img {
  width: 100%;
  height: auto;
  object-fit: cover;
  border-radius: 8px;
}

.post-img.small {
  aspect-ratio: 1/1;
}

.more-images {
  position: absolute;
  bottom: 4px;
  right: 4px;
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.post-video {
  width: 100%;
  border-radius: 8px;
  overflow: hidden;
}

.post-video-player {
  width: 100%;
  height: auto;
}

.post-actions {
  display: flex;
  gap: 24px;
  padding-top: 12px;
  border-top: 1px solid #f0f0f0;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px 0;
  color: #666;
  font-size: 14px;
  transition: color 0.2s;
}

.action-btn:hover {
  color: #ff0000;
}

.like-btn.liked {
  color: #ff0000;
}

.like-icon, .comment-icon {
  font-size: 20px;
}

@media (max-width: 768px) {
  .social-header {
    padding: 12px 16px;
  }
  
  .posts-feed {
    padding: 12px;
  }
  
  .post-card {
    padding: 12px;
    border-radius: 8px;
  }
}
</style>